﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using HA.ModFlow.Infrastructure;

using HAUtilWpf;

using HA.ModFlow.Controls.UserControls;

using System.Data;

using HA.ModFlow.Controls.MT3DMSWindows;

using HA.Data;



namespace HA.ModFlow.Controls
{
    /// <summary>
    /// winCellCenterDSEdit.xaml 的交互逻辑，这个类被用于交互cellcenterdataset的编辑，目前还不确定是否启用
    /// </summary>
    public partial class winCellCenterDSEdit : Window
    {




        public winCellCenterDSEdit(CellCenterDataSet3d dataset, Project proj, bool isDefault = true)
        {
            InitializeComponent();
            this.Project = proj;


            this._pcDs = new PCCellCenterDS(dataset);

            this.Title = "数据  "+ HAUtil.LanguageTranslater.ENToCN( dataset.Label,isDefault);


            #region 初始化datagrid的一些属性

            this.dgData.CanUserAddRows = false;
            //this.dgData.CanUserPasteToNewRows = false;
            this.dgData.GridLinesVisibility = DataGridGridLinesVisibility.None;

            this.dgData.SelectionUnit = DataGridSelectionUnit.Cell;





            //右键菜单

            MenuItem itemCopy = new MenuItem();
            itemCopy.Header = "复制";
            itemCopy.Command = ApplicationCommands.Copy;

            //MenuItem itemPaste = new MenuItem();
            //itemPaste.Header = "粘贴";
            //itemPaste.Command = ApplicationCommands.Paste;

            MenuItem itemPaste2 = new MenuItem();
            itemPaste2.Header = "粘贴";
            itemPaste2.InputGestureText = "Ctrl+V";
            itemPaste2.Click += new RoutedEventHandler(itemPaste2_Click);

            ContextMenu cm = new ContextMenu();
            cm.Items.Add(itemCopy);
            cm.Items.Add(itemPaste2);
            //cm.Items.Add(itemPaste2);
            this.dgData.ContextMenu = cm;



            this.dgData.LoadingRow += new EventHandler<DataGridRowEventArgs>(dgData_LoadingRow);

            this.dgData.AutoGeneratingColumn += new EventHandler<DataGridAutoGeneratingColumnEventArgs>(dgData_AutoGeneratingColumn);

            this.dgData.AutoGeneratedColumns += new EventHandler(dgData_AutoGeneratedColumns);



            #endregion


            this.DataContext = this._pcDs;

            this.labelUnit.DataContext = this;


            //初始化数据

            this.InitData();



        }



     




        protected void InitData()
        {


            //this._layerIndex.Clear();

            //for (int i = 0; i < datas.Count; i++)
            //{
            //    this._layerIndex.Add(i + 1);
            //}


            //this._type = DataType.DOUBLE;

            //this._doubleData = datas;


            //this.cbLayerIndex.SelectedIndex = 0;
 

        }

        #region 属性数据


        string _unit = "   单位:";

        public string Unit
        {
            get { return _unit; }
            set { _unit = value; }
        }


        PCCellCenterDS _pcDs;

        public PCCellCenterDS PcDs
        {
            get { return _pcDs; }
            set { _pcDs = value; }
        }

      

       Project _project;

        public Project Project
        {
          get { return _project; }
          set { _project = value; }
        }
      
        
        

        //正常的数据

        PCGridArray<double> _arrays;

        public PCGridArray<double> Arrays
        {
            get { return _arrays; }
            set
            {
                _arrays = value;

                this.OnSetGridArray();

            }
        }


        protected void OnSetGridArray()
        {

            this.dgData.Columns.Clear();

        }



        


        DataType _type;

        public DataType Type
        {
            get { return _type; }
            set { _type = value; }
        }

     

     


        #endregion


        void dgData_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
        {
            DataGridTextColumn column = e.Column as DataGridTextColumn;
            Binding binding = column.Binding as Binding;
            binding.Path = new PropertyPath(binding.Path.Path + ".Value");
            //if (this._type == DataType.DOUBLE)
            //{
            //    binding.Converter = new NumberToStringConverter();
            //}

        }

        void dgData_AutoGeneratedColumns(object sender, EventArgs e)
        {
            for (int i = 0; i < this.dgData.Columns.Count; i++)
            {
                this.dgData.Columns[i].Header = (i + 1).ToString();
                //dgData.SetResourceReference(DataGrid.ColumnHeaderStyleProperty, "DataGridHeaderStyle");
                //dgData.SetResourceReference(DataGrid.CellStyleProperty, "DataGridCellStyle");
            }
        }


        //配置datagrid


        void dgData_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            e.Row.Header = (e.Row.GetIndex() + 1).ToString();
        }









        private void itemPaste2_Click(object sender, RoutedEventArgs e)
        {
            //取得剪贴板数据


            List<string[]> rowData = ClipboardHelper.ParseClipboardData();

            if (rowData==null||rowData.Count==0)
            {
                return;
              
                
            }

            //取得选中单元的行序号

            int minRowIndex = DataGridHelper.GetRowIndex(this.dgData.SelectedCells[0]);

            //下面的代码写的太过蹊跷，应该取得的是选中的行号吧?


            int maxRowIndex = this.dgData.Items.Count - 1;

            int minColumnDisplayIndex = this.dgData.SelectedCells[0].Column.DisplayIndex;

            int maxColumnDisplayIndex = this.dgData.Columns.Count - 1;



            int rowDataIndex = 0;
            if (this._type == DataType.INT)
            {
                for (int i = minRowIndex; i <= maxRowIndex && rowDataIndex < rowData.Count; i++, rowDataIndex++)
                {
                    int columnDataIndex = 0;
                    for (int j = minColumnDisplayIndex; j <= maxColumnDisplayIndex && columnDataIndex < rowData[rowDataIndex].Length; j++, columnDataIndex++)
                    {
                        if (rowData[rowDataIndex][columnDataIndex] != "")
                        {

                            //this._intData[i, j] = Convert.ToInt32(rowData[rowDataIndex][columnDataIndex]);
                        }
                    }
                }
                this.dgData.Items.Refresh();
            }
            else
            {
                for (int i = minRowIndex; i <= maxRowIndex && rowDataIndex < rowData.Count; i++, rowDataIndex++)
                {
                    int columnDataIndex = 0;
                    for (int j = minColumnDisplayIndex; j <= maxColumnDisplayIndex && columnDataIndex < rowData[rowDataIndex].Length; j++, columnDataIndex++)
                    {
                        if (rowData[rowDataIndex][columnDataIndex] != "")
                        {
                            double value=0;

                            bool b = double.TryParse(rowData[rowDataIndex][columnDataIndex],out value);

                            if (b)
                            {

                                this._pcDs.CurrentLayerDouble[i, j] = value;



                            }

                            else
                            {
                                continue;
                               
                            }

                           
                           
                        }
                    }
                }
                this.dgData.Items.Refresh();
            }
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;

            this._pcDs.Confirm();

            this.Close();



        }

        private void btnResetGrid_Click(object sender, RoutedEventArgs e)
        {
            winResetValue winReset = new winResetValue(this._type);
            winReset.Owner = this;
            winReset.ShowDialog();

            if (winReset.DialogResult == true)
            {

                this._pcDs.ResetDoubleData(Convert.ToDouble(winReset.txtResetValue.Text));

                this.dgData.Items.Refresh();



                //if (this._type == DataType.INT)
                //{
                //    ResetIntData(Convert.ToInt32(winReset.txtResetValue.Text));
                //}
                //else
                //    ResetDoubleData(Convert.ToDouble(winReset.txtResetValue.Text));
            }

        }

        private void btnResetLayer_Click(object sender, RoutedEventArgs e)
        {

             winResetValue winReset = new winResetValue(this._type);
             winReset.Owner = this;
            winReset.ShowDialog();

            if (winReset.DialogResult == true)
            {

                this._pcDs.ResetDoubleDataCurrentLayer(Convert.ToDouble(winReset.txtResetValue.Text));

                this.dgData.Items.Refresh();

            }

        }

        private void btnResetPart_Click(object sender, RoutedEventArgs e)
        {
            if (this.dgData.SelectedCells.Count == 0)
            {
                //winAlert winAlert = new winAlert();
                //winAlert.ShowDialog();

                MessageBox.Show("请选择需要编辑的单元选区！");



            }
            else
            {
                winResetValue winPartReset = new winResetValue(this._type);
                winPartReset.Owner = this;
                winPartReset.ShowDialog();

                if (winPartReset.DialogResult == true)
                {
                    double value = Convert.ToDouble(winPartReset.txtResetValue.Text);




                    IList<DataGridCellInfo> selectedCells = this.dgData.SelectedCells;

                    int index = this._pcDs.SelectedIndex;

                    int count = this.dgData.SelectedCells.Count;

                    for (int i = 0; i < count; i++)
                    {

                        DataRowView drv = (DataRowView)selectedCells[i].Item;
                        DataTable dt = drv.Row.Table;
                        int rowIndex = dt.Rows.IndexOf(drv.Row);


                        int column = selectedCells[i].Column.DisplayIndex;

                        this._pcDs.LstData[index][rowIndex, column] = value;
                        



                    }




                 

                    this.dgData.Items.Refresh();


                    //if (this._type == DataType.INT)
                    //{
                    //    ResetPartIntData((int)value);
                    //}
                    //else
                    //    ResetPartDoubleData(value);

                }
            }
        }

        private void btnImportData_Click(object sender, RoutedEventArgs e)
        {
            if (this._project==null)
            {
                return;
                
            }

             winSelectDataSet win = new winSelectDataSet(_project.Grid3dData.MainGrid.DatasetCollecion);
            win.SelectDS.UseAllTimeStep = false;

           
            win.Owner = this;
            win.ResizeMode = ResizeMode.NoResize;
            win.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            if (win.ShowDialog() == true)
            {

                ///当用户确实选择了一定的数据集时
                if (win.SelectedDsGrid != null && win.SelectedDataSet != null)
                {

                    //更新当期的 CellCenterDataSet3d  数据

                    CellCenterDataSet3d newCellCenterDataSet = win.SelectedDataSet;

                    if (newCellCenterDataSet == null)
                    {

                        System.Windows.Forms.MessageBox.Show("您选择的数据集为 null ！");
                    }
                    else
                    {
                        if (newCellCenterDataSet.Datas != null)
                        {
                            this._pcDs.Reset3DArryDataToListData(newCellCenterDataSet.Datas);

                            this.dgData.Items.Refresh();
                        }
                     
                    
                    }
                  

                }

            }



        }





    }


   
}
